import type { PredictionResult, PredictionDetail, ModelMetrics } from '@/api/prediction'

// 生成模拟预测数据
export const generateMockPredictionData = (timeRange: number): PredictionResult => {
  const details: PredictionDetail[] = []
  const today = new Date()
  
  // 生成历史数据（过去30天）
  const historicalData = []
  for (let i = 30; i > 0; i--) {
    const date = new Date(today)
    date.setDate(date.getDate() - i)
    historicalData.push({
      date: date.toISOString().split('T')[0],
      outbound: Math.floor(Math.random() * 100) + 50,
      inbound: Math.floor(Math.random() * 80) + 40,
      goods: Math.floor(Math.random() * 200) + 100
    })
  }
  
  // 生成预测数据
  for (let i = 1; i <= timeRange; i++) {
    const date = new Date(today)
    date.setDate(date.getDate() + i)
    
    // 基于历史数据生成趋势性预测
    const baseOutbound = 75 + Math.sin(i * 0.1) * 20 + Math.random() * 10
    const baseInbound = 60 + Math.cos(i * 0.1) * 15 + Math.random() * 8
    const baseGoods = 150 + Math.sin(i * 0.05) * 30 + Math.random() * 15
    
    details.push({
      date: date.toISOString().split('T')[0],
      outbound: Math.max(0, Math.floor(baseOutbound)),
      inbound: Math.max(0, Math.floor(baseInbound)),
      goods: Math.max(0, Math.floor(baseGoods)),
      confidence: 0.7 + Math.random() * 0.25, // 0.7-0.95
      modelAccuracy: 0.75 + Math.random() * 0.2, // 0.75-0.95
      historicalValue: i <= 7 ? historicalData[historicalData.length - 7 + i]?.outbound : undefined,
      notes: i % 7 === 0 ? '周末预测' : ''
    })
  }
  
  // 计算总趋势
  const avgOutbound = details.reduce((sum, item) => sum + item.outbound, 0) / details.length
  const avgInbound = details.reduce((sum, item) => sum + item.inbound, 0) / details.length
  const avgGoods = details.reduce((sum, item) => sum + item.goods, 0) / details.length
  
  const historicalAvgOutbound = historicalData.reduce((sum, item) => sum + item.outbound, 0) / historicalData.length
  const historicalAvgInbound = historicalData.reduce((sum, item) => sum + item.inbound, 0) / historicalData.length
  const historicalAvgGoods = historicalData.reduce((sum, item) => sum + item.goods, 0) / historicalData.length
  
  const modelMetrics: ModelMetrics = {
    r2: 0.85 + Math.random() * 0.1, // 0.85-0.95
    mae: 5 + Math.random() * 3, // 5-8
    rmse: 8 + Math.random() * 4, // 8-12
    trainingTime: 2 + Math.random() * 3 // 2-5秒
  }
  
  return {
    totalOutbound: Math.floor(avgOutbound * timeRange),
    totalInbound: Math.floor(avgInbound * timeRange),
    totalGoods: Math.floor(avgGoods * timeRange),
    outboundTrend: Math.round(((avgOutbound - historicalAvgOutbound) / historicalAvgOutbound) * 100),
    inboundTrend: Math.round(((avgInbound - historicalAvgInbound) / historicalAvgInbound) * 100),
    goodsTrend: Math.round(((avgGoods - historicalAvgGoods) / historicalAvgGoods) * 100),
    details,
    modelMetrics
  }
}

// 根据模型类型调整预测结果
export const adjustPredictionByModel = (data: PredictionResult, modelType: string): PredictionResult => {
  const adjusted = { ...data }
  
  switch (modelType) {
    case 'linear':
      // 线性回归模型 - 更保守的预测
      adjusted.details = adjusted.details.map(item => ({
        ...item,
        outbound: Math.floor(item.outbound * 0.9),
        inbound: Math.floor(item.inbound * 0.9),
        goods: Math.floor(item.goods * 0.9),
        confidence: item.confidence * 0.95
      }))
      adjusted.modelMetrics.r2 *= 0.9
      break
      
    case 'randomForest':
      // 随机森林模型 - 保持原预测
      break
      
    case 'neuralNetwork':
      // 神经网络模型 - 更激进的预测
      adjusted.details = adjusted.details.map(item => ({
        ...item,
        outbound: Math.floor(item.outbound * 1.1),
        inbound: Math.floor(item.inbound * 1.1),
        goods: Math.floor(item.goods * 1.1),
        confidence: Math.min(0.95, item.confidence * 1.05)
      }))
      adjusted.modelMetrics.r2 *= 1.05
      break
      
    case 'timeSeries':
      // 时间序列模型 - 考虑季节性
      adjusted.details = adjusted.details.map((item, index) => {
        const seasonalFactor = 1 + 0.2 * Math.sin((index + 1) * Math.PI / 14) // 两周周期
        return {
          ...item,
          outbound: Math.floor(item.outbound * seasonalFactor),
          inbound: Math.floor(item.inbound * seasonalFactor),
          goods: Math.floor(item.goods * seasonalFactor),
          confidence: Math.min(0.95, item.confidence * 1.02)
        }
      })
      adjusted.modelMetrics.r2 *= 1.02
      break
  }
  
  return adjusted
}

// 生成商品类型列表
export const generateGoodsTypes = () => {
  return [
    { goodstypecoding: '1', wGoodstypename: '电子产品' },
    { goodstypecoding: '2', wGoodstypename: '服装鞋帽' },
    { goodstypecoding: '3', wGoodstypename: '食品饮料' },
    { goodstypecoding: '4', wGoodstypename: '家居用品' },
    { goodstypecoding: '5', wGoodstypename: '图书文具' },
    { goodstypecoding: '6', wGoodstypename: '运动户外' },
    { goodstypecoding: '7', wGoodstypename: '美妆护肤' },
    { goodstypecoding: '8', wGoodstypename: '母婴用品' }
  ]
} 